<template>
  <div>
    <h1 class="title">用户登录</h1>
    <el-form :model="ruleForm" status-icon ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名">
          <el-input type="text" v-model="ruleForm.userName"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleForm)">登录</el-button>
        </el-form-item>
    </el-form>
  </div>
</template>

<script>
  export default {
    name:"login",
    data(){
      return {
        ruleForm:{
          userName:"小明",
          password:123456,
        }
      }
    },
    methods:{
      submitForm(values){
        //假设登录成功
        //缓存，token/
        if(values.userName==="111111" && values.password==="111111"){
        const userInfo = {
        userName: "111111",
        avatar:
          "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3262544826,1690254880&fm=26&gp=0.jpg",
        role:"admin"
        };
      //存一个token
        const accessToken = "sodjunvjfkrutjgkti";
        localStorage.setItem("userInfo", JSON.stringify(userInfo));
        localStorage.setItem("accessToken", accessToken);
       
        if (this.$route.params) {
        //如果有参数，就去指定params.path
        this.$router.push(this.$route.params.path);
        } else {
        // 否则就去首页
        this.$router.push("/admin");
        }
      }else{
        alert('您输入的密码或用户名错误，请重新登录')
      }
      }
    }

  }
</script>

<style lang="scss" scoped>
.title{
  text-align:center
}

</style>